//
//  CLShareViewController.m
//  chanlin
//
//  Created by Ryan on 14-12-10.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLShareViewController.h"
#import "BlockUI.h"
#import "CLGlobleDataModel.h"

#define CHTumblrMenuViewImageHeight 83
#define CHTumblrMenuViewTitleHeight 20
#define CHTumblrMenuViewVerticalPadding 10
#define CHTumblrMenuViewHorizontalMargin 10
#define CHTumblrMenuViewRriseAnimationID @"CHTumblrMenuViewRriseAnimationID"
#define CHTumblrMenuViewDismissAnimationID @"CHTumblrMenuViewDismissAnimationID"
#define CHTumblrMenuViewAnimationTime 0.5
#define CHTumblrMenuViewAnimationInterval (CHTumblrMenuViewAnimationTime / 5)

@interface CLShareViewController ()

@property (nonatomic, strong) NSMutableArray *buttons;

@end

@implementation CLShareViewController

- (void)viewDidLoad{
    
    self.shareView = [[UIShareView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:self.shareView];
    
    if(AtLeastIOS8){
        
        UIVisualEffectView *effectview=[[UIVisualEffectView alloc]
                                        initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
        
        effectview.frame=self.shareView.bounds;
        [self.shareView addSubview:effectview];
        
    }else{
        UIView *bg=[UIView new];
        bg.backgroundColor = [UIColor blackColor];
        bg.alpha=0.65;
        bg.frame=self.shareView.bounds;
        [self.shareView addSubview:bg];
    }
    _buttons = [[NSMutableArray alloc] initWithCapacity:6];
    
    [self createButtonView];
    [self riseAnimation];
    [self cancelBtn];
}

- (void)createButtonView {
    self.weixin = [[CLShareButton alloc]initWithTitle:@"微信" andIcon:[UIImage imageNamed:@"share_weixin.png"]];
    
    self.weixin.backgroundColor = [UIColor clearColor];
    self.weixin.frame = CGRectMake(4, 20, 79, 83);
    [self.view addSubview:self.weixin];
    [_buttons addObject:self.weixin];
    
    self.pengyou = [[CLShareButton alloc]initWithTitle:@"朋友圈" andIcon:[UIImage imageNamed:@"share_pengyouquan.png"]];
    self.pengyou.backgroundColor = [UIColor clearColor];
    self.pengyou.frame = CGRectMake(4+79, 20, 79, 83);
    [self.view addSubview:self.pengyou];
    [_buttons addObject:self.pengyou];
    
    if ([QQApiInterface isQQInstalled]) {
        self.qq = [[CLShareButton alloc]initWithTitle:@"QQ" andIcon:[UIImage imageNamed:@"share_QQ.png"]];
        self.qq.titleLabel = self.qqLable;
        self.qq.backgroundColor = [UIColor clearColor];
        self.qq.frame = CGRectMake(4+79*2, 20, 79, 83);
        [self.view addSubview:self.qq];
        [_buttons addObject:self.qq];
        
    }
    
    self.sina = [[CLShareButton alloc]initWithTitle:@"新浪微博" andIcon:[UIImage imageNamed:@"share_sina.png"]];
    self.sina.backgroundColor = [UIColor clearColor];
    self.sina.frame = CGRectMake(4+79*([QQApiInterface isQQInstalled]?3:2), 20, 79, 83);
    [self.view addSubview:self.sina];
    [_buttons addObject:self.sina];
}

- (void)riseAnimation
{
    NSUInteger columnCount = _buttons.count;
    NSUInteger rowCount = _buttons.count / columnCount + (_buttons.count%columnCount>0?1:0);
    
    for (NSUInteger index = 0; index < _buttons.count; index++) {
        CLShareButton *button = _buttons[index];
        button.layer.opacity = 0;
        CGRect frame = [self frameForButtonAtIndex:index];
        NSUInteger rowIndex = index / columnCount;
        CGPoint fromPosition = CGPointMake(frame.origin.x + CHTumblrMenuViewImageHeight / 2.0,frame.origin.y +  (rowCount - rowIndex + 2)*200 + (CHTumblrMenuViewImageHeight + CHTumblrMenuViewTitleHeight) / 2.0);
        
        CGPoint toPosition = CGPointMake(frame.origin.x + CHTumblrMenuViewImageHeight / 2.0,frame.origin.y + (CHTumblrMenuViewImageHeight + CHTumblrMenuViewTitleHeight) / 2.0);
        
        double delayInSeconds = index * CHTumblrMenuViewAnimationInterval;
    
        CABasicAnimation *positionAnimation;
        
        positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        positionAnimation.fromValue = [NSValue valueWithCGPoint:fromPosition];
        positionAnimation.toValue = [NSValue valueWithCGPoint:toPosition];
        positionAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.45f :1.2f :0.75f :1.0f];
        positionAnimation.duration = CHTumblrMenuViewAnimationTime;
        positionAnimation.beginTime = [button.layer convertTime:CACurrentMediaTime() fromLayer:nil] + delayInSeconds;
        [positionAnimation setValue:[NSNumber numberWithUnsignedInteger:index] forKey:CHTumblrMenuViewRriseAnimationID];
        positionAnimation.delegate = self;
        
        [button.layer addAnimation:positionAnimation forKey:@"riseAnimation"];
   
    }
}

- (CGRect)frameForButtonAtIndex:(NSUInteger)index
{
    CGFloat offsetX = 0;
    offsetX += 79*index;
    return CGRectMake(offsetX, 5, 79, 83);
    
}

- (void)dropAnimation{
    
    NSUInteger columnCount = _buttons.count;
    for (NSUInteger index = 0; index < _buttons.count; index++) {
        CLShareButton *button = _buttons[index];
        CGRect frame = [self frameForButtonAtIndex:index];
        
        NSUInteger rowIndex = index / columnCount;
        
        CGPoint toPosition = CGPointMake(frame.origin.x + CHTumblrMenuViewImageHeight / 2.0,frame.origin.y +  (rowIndex + 2)*200 - (CHTumblrMenuViewImageHeight - CHTumblrMenuViewTitleHeight) / 2.0);
        
        CGPoint fromPosition = CGPointMake(frame.origin.x + CHTumblrMenuViewImageHeight / 2.0,frame.origin.y + (CHTumblrMenuViewImageHeight + CHTumblrMenuViewTitleHeight) / 2.0);
        
        double delayInSeconds = (_buttons.count-1-index) * 0.15;

        
        CABasicAnimation *positionAnimation;
        
        positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        positionAnimation.fromValue = [NSValue valueWithCGPoint:fromPosition];
        positionAnimation.toValue = [NSValue valueWithCGPoint:toPosition];
        positionAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.45f :1.2f :0.75f :1.0f];
        positionAnimation.duration = 0.15*_buttons.count;
        positionAnimation.beginTime = [button.layer convertTime:CACurrentMediaTime() fromLayer:nil] + delayInSeconds;
        [positionAnimation setValue:[NSNumber numberWithUnsignedInteger:index] forKey:CHTumblrMenuViewDismissAnimationID];
        positionAnimation.delegate = self;
        
        [button.layer addAnimation:positionAnimation forKey:@"riseAnimation"];
 
    }
    
    double delayInSeconds = CHTumblrMenuViewAnimationInterval * (_buttons.count + 1);
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shareViewDisappear" object:nil];
    });
    
}

- (void)animationDidStart:(CAAnimation *)anim
{
    if([anim valueForKey:CHTumblrMenuViewRriseAnimationID]) {
        NSUInteger index = [[anim valueForKey:CHTumblrMenuViewRriseAnimationID] unsignedIntegerValue];
        UIView *view = _buttons[index];
        CGRect frame = [self frameForButtonAtIndex:index];
        CGPoint toPosition = CGPointMake(frame.origin.x + CHTumblrMenuViewImageHeight / 2.0,frame.origin.y + (CHTumblrMenuViewImageHeight + CHTumblrMenuViewTitleHeight) / 2.0);
        CGFloat toAlpha = 1.0;
        
        view.layer.position = toPosition;
        view.layer.opacity = toAlpha;
        
    }
    else if([anim valueForKey:CHTumblrMenuViewDismissAnimationID]) {
        NSUInteger index = [[anim valueForKey:CHTumblrMenuViewDismissAnimationID] unsignedIntegerValue];
        
        UIView *view = _buttons[index];
        CGRect frame = [self frameForButtonAtIndex:index];
        
        CGPoint toPosition = CGPointMake(frame.origin.x + CHTumblrMenuViewImageHeight / 2.0,frame.origin.y + GlobleHeight);
        
        view.layer.position = toPosition;
    }
}

- (void)creatLabel:(UIButton *)button withTitle:(NSString *)title{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 66, 79, 13)];
    label.backgroundColor = [UIColor clearColor];
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    label.font= [UIFont systemFontOfSize:13.f];
    label.textColor = RGB(0xcc, 0xcc, 0xcc);
    [button addSubview:label];
}

- (void)cancelBtn {
    self.cancle = [[UIButton alloc]initWithFrame:CGRectMake(0, 128, GlobleWidth, 49)];
    [self.shareView addSubview:self.cancle];
    
    UIImageView *seprateLine = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, GlobleWidth, 0.5)];
    seprateLine.backgroundColor = RGB(0xcc, 0xcc, 0xcc);
    [self.cancle addSubview:seprateLine];
    
    UILabel *cancelLab = [[UILabel alloc]initWithFrame:CGRectMake((GlobleWidth-40)/2, (49-15)/2, 40, 15)];
    cancelLab.text = @"取消";
    cancelLab.font = [UIFont systemFontOfSize:15.f];
    cancelLab.textAlignment = NSTextAlignmentCenter;
    cancelLab.textColor = RGB(0xcc, 0xcc, 0xcc);
    [self.cancle addSubview:cancelLab];
}

@end
